package com.three.digit;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import com.three.digit.model.D3JComboBox;
import com.three.digit.model.Point;
import com.three.digit.tools.Utils;

public class HomePage extends ApplicationFrame
{
    private static final long serialVersionUID = 1L;

    public HomePage (String title)
    {
        super (title);
        setContentPane (new XYZTabbedPane ());
    }

    public static void main (String[] args)
    {
        HomePage homePage = new HomePage ("3D");

        homePage.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        homePage.setSize (600, 400);
        // homePage.pack ();
        RefineryUtilities.centerFrameOnScreen (homePage);
        homePage.setVisible (true);
    }

    static class XYZTabbedPane extends JTabbedPane implements ActionListener
    {
        private Point point;
        private static HashMap <Integer, Point> map;
        private JComboBox start;
        private JComboBox end;
        
        public XYZTabbedPane ()
        {
            super ();
            
            this.point = Utils.readPoint("BackUpSanDHistory.txt");
            this.map = Utils.toHashMap (Utils.readPoints ("BackUpSanDHistory.txt"));
            
            JPanel operContain = createOperArea ();
        }
        
        public JPanel createOperArea() {
            start = new D3JComboBox(point);
            start.setSize(75, 25);

            end = new D3JComboBox(point);
            end.setSize(75, 25);

            JButton jButton = new JButton("Generate");
            jButton.addActionListener(this);

            JPanel operContain = new JPanel(new FlowLayout());
            operContain.add(new JLabel("Start"));
            operContain.add(start);
            operContain.add(new JLabel("End"));
            operContain.add(end);
            operContain.add(jButton);

            return operContain;
        }

        public JPanel createDemoPanel (CategoryDataset dataSet)
        {
            JFreeChart localJFreeChart = createChart (dataSet);
            JPanel jPanel = new ChartPanel (localJFreeChart);
            jPanel.setName ("XYZ-Total");
            return jPanel;
        }

        private CategoryDataset createDataset (Point point, int start, int end)
        {
            DefaultCategoryDataset dcd = new DefaultCategoryDataset ();

            do
            {
                if (point.getId () >= start)
                {
                    dcd.addValue (point.span (), "Span", point.getId () + "");
                    dcd.addValue (point.sum (), "Sum", point.getId () + "");
                    dcd.addValue (point.getX (), "X", point.getId () + "");
                    dcd.addValue (point.getY (), "Y", point.getId () + "");
                    dcd.addValue (point.getZ (), "Z", point.getId () + "");
                }
            }
            while (((point = point.next) != null && point.getId () <= end));

            return dcd;
        }

        private static JFreeChart createChart (CategoryDataset paramXYDataset)
        {
            JFreeChart jFreeChart = ChartFactory.createLineChart (null, "Year", "Value", paramXYDataset,
                                                                  PlotOrientation.VERTICAL, true, true, false);

            CategoryPlot categoryPlot = (CategoryPlot) jFreeChart.getPlot ();

            NumberAxis numberAxis = (NumberAxis) categoryPlot.getRangeAxis ();
            numberAxis.setStandardTickUnits (NumberAxis.createIntegerTickUnits ());

            CategoryAxis categoryAxis = categoryPlot.getDomainAxis ();
            categoryAxis.setCategoryLabelPositions (CategoryLabelPositions.UP_45);

            LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryPlot.getRenderer ();

            renderer.setBaseToolTipGenerator (new CategoryToolTipGenerator ()
            {

                public String generateToolTip (CategoryDataset dataset, int row, int column)
                {

                    Point p = map.get (Integer.valueOf (dataset.getColumnKey (column).toString ()));

                    return dataset.getColumnKey (column) + " " + p.getPresent () + " " + dataset.getRowKey (row)
                           + " = " + dataset.getValue (row, column).intValue ();
                }
            });

            renderer.setBaseShapesVisible (true);
            renderer.setDrawOutlines (true);
            renderer.setUseFillPaint (true);
            renderer.setBaseFillPaint (Color.white);

            return jFreeChart;
        }

        public void actionPerformed (ActionEvent e)
        {
            Object object = e.getSource ();
            if (object instanceof JButton)
            {
                int startIndex = (Integer) start.getSelectedItem ();
                int endIndex = (Integer) end.getSelectedItem ();

                JButton jButton = (JButton) object;
                jButton.setText ("Generate - " + (Math.abs (end.getSelectedIndex () - start.getSelectedIndex ()) + 1));

                removeAll ();
//                add (createDemoPanel (point, startIndex, endIndex));
            }
            
            
        }
    }

}
